home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Uip / rcvalert / ttysbr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  2.1 KB  |  114 lines

  1. /* ttysbr.c: */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Uip/rcvalert/RCS/ttysbr.c,v 6.0 1991/12/18 20:39:41 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Uip/rcvalert/RCS/ttysbr.c,v 6.0 1991/12/18 20:39:41 jpo Rel $
  9.  *
  10.  * $Log: ttysbr.c,v $
  11.  * Revision 6.0  1991/12/18  20:39:41  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include <config.h>
  19. #include <isode/manifest.h>
  20. #include <stdio.h>
  21. #include <utmp.h>
  22. #include <fcntl.h>
  23. #include <sys/stat.h>
  24.  
  25. #ifndef UTMP_FILE
  26. #define UTMP_FILE    "/etc/utmp"
  27. #endif
  28. #ifdef XOS_2
  29. struct utmp *getutent();
  30. #endif
  31.  
  32. #define OKTONOTIFY    020    /* group write perm */
  33.  
  34. #ifdef nonuser
  35. #define NONUSER(x)    nonuser(x)
  36. #else
  37. #ifdef XOS_2
  38. #define NONUSER(x)    ((x)->ut_user[0] == 0)
  39. #else
  40. #define NONUSER(x)    ((x).ut_name[0] == 0)
  41. #endif
  42. #endif
  43.  
  44. int notify_normal (str, username)
  45. char    *str;
  46. char    *username;
  47. {
  48.     FILE    *fp;
  49.     int    done = NOTOK;
  50. #ifdef XOS_2
  51.     struct utmp *utmp;
  52. #else
  53.     struct utmp utmp;
  54. #endif
  55.  
  56. #ifdef XOS_2
  57.     while ((utmp = getutent ()) != (struct utmp *)NULL) {
  58.         if (NONUSER (utmp))
  59.             continue;
  60.         if (strncmp (utmp->ut_user, username,
  61.                  sizeof (utmp->ut_user)) == 0)
  62.             if (notify_user (utmp, str) == OK)
  63.                 done = OK;
  64.     }
  65. #else
  66.     if ((fp = fopen (UTMP_FILE, "r")) == NULL)
  67.         return NOTOK;
  68.  
  69.     while ((int)fread ((char *)&utmp, sizeof utmp, 1, fp) > 0) {
  70.         if (NONUSER (utmp))
  71.             continue;
  72.         if (strncmp (utmp.ut_name, username,
  73.                  sizeof (utmp.ut_name)) == 0)
  74.             if (notify_user (&utmp, str) == OK)
  75.                 done = OK;
  76.     }
  77.     (void) fclose (fp);
  78. #endif
  79.     return done;
  80. }
  81.  
  82. notify_user (ut, str)
  83. struct utmp *ut;
  84. char    *str;
  85. {
  86.     char device[BUFSIZ];
  87.     struct stat st;
  88.     int    pid;
  89.     int    fd;
  90.  
  91.     (void) sprintf (device, "/dev/%.*s", sizeof (ut -> ut_line),
  92.             ut -> ut_line);
  93.  
  94.     if (stat (device, &st) == NOTOK)
  95.         return NOTOK;
  96.     if ((st.st_mode & OKTONOTIFY) == 0)
  97.         return NOTOK;
  98.  
  99.     if ((pid = fork ()) == NOTOK)
  100.         return NOTOK;
  101.  
  102.     if (pid)
  103.         return OK;
  104.  
  105.     if ((fd = open (device, O_WRONLY, 0)) == NOTOK)
  106.         _exit(1);
  107.     (void) write (fd, "\r\n\007", 3);
  108.     (void) write (fd, str, strlen (str));
  109.     (void) write (fd, "\r\n\007", 3);
  110.     (void) close (fd);
  111.     _exit (0);
  112.     return NOTOK;
  113. }
  114.